package com.vitabuybuy.tv.ui.auth

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.ext.mmkv
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.toast.XToast

import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityFindPasswordBinding
import com.vitabuybuy.tv.ui.auth.viewmodel.AuthViewModel
import com.library.common.util.MMKVContancts
import com.vitabuybuy.tv.dialog.AreaCodePopup
import kotlinx.android.synthetic.main.activity_find_password.*

class FindPasswordActivity : BaseActivity<AuthViewModel, ActivityFindPasswordBinding>(){
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_find_password

    private var fromAction: String? = ""
    private var isGetCode = false
    private var defaultAreaCode = "86"

    override fun onGetBundle(bundle: Bundle?) {
        super.onGetBundle(bundle)
        fromAction = bundle?.getString("fromAction")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(if (fromAction == FORGET_PWD) getString(R.string.find_password) else getString(R.string.change_password))
        if (fromAction == RESET_PWD) {
            etPhone.setText(mmkv.decodeString(MMKVContancts.USER_PHONE, ""))
            etPhone.isEnabled = false
        }
    }

    override fun eventHandling() {
        setOnclickNoRepeat(tvGetSmsCode, btnNext) { v ->
            if (etPhone.text.isEmpty()) {
                XToast.normal(getString(R.string.enter_phone_number))
                return@setOnclickNoRepeat
            }
            if (mmkv.decodeBool(MMKVContancts.IS_LOGIN_IN, false)) {
                if (etPhone.text.toString() != mmkv.decodeString(MMKVContancts.USER_PHONE, "")) {
                    XToast.normal(getString(R.string.pls_user_current_phone))
                    return@setOnclickNoRepeat
                }
            }
            when (v) {
                tvGetSmsCode -> {
                    viewModel.sendToPhone(fromAction, etPhone.text.toString(), defaultAreaCode)
                }
                btnNext -> {
                    if (!isGetCode) {
                        XToast.normal(getString(R.string.please_get_code))
                        return@setOnclickNoRepeat
                    }
                    if (etSmsCode.text.isEmpty()) {
                        XToast.normal(getString(R.string.enter_code))
                        return@setOnclickNoRepeat
                    }
                    viewModel.validateByPhone(
                        etPhone.text.toString(), etSmsCode.text.toString(),
                        defaultAreaCode
                    )
                }
            }
        }
        setOnclickNoRepeat(countryCodePicker) {
            AreaCodePopup(this) {
                countryCodePicker.text = it
                defaultAreaCode = it.replace("+", "")
            }.showPopupWindow()
        }
    }


    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                XToast.success(getString(R.string.already_sms_code))
                timer.start()
            }
            2 -> {
                ChangePasswordActivity.openActivity(
                    mContext,
                    etPhone.text.toString(),
                    defaultAreaCode,
                    ChangePasswordActivity.PHONE_RESET
                )
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        if (requestCode == 1001) {
            finish()
        }
    }

    private val timer = object : CountDownTimer((60 * 1000).toLong(), 1000) {
        override fun onTick(millisUntilFinished: Long) {
            isGetCode = true
            mBinding.tvGetSmsCode.text = String.format(
                getString(R.string.get_sms_code_s),
                millisUntilFinished / 1000
            )
            mBinding.tvGetSmsCode.isEnabled = false
        }

        override fun onFinish() {
            mBinding.tvGetSmsCode.text = getString(R.string.get_sms_code)
            mBinding.tvGetSmsCode.isEnabled = true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }

    companion object {
        val FORGET_PWD = "forget_pwd"
        val RESET_PWD = "reset_pwd"
        fun openActivity(mContext: BaseActivity<*, *>, fromAction: String) {
            val bundle = Bundle()
            bundle.putString("fromAction", fromAction)
            mContext.startActivity(bundle, FindPasswordActivity::class.java)
        }
    }
}
